<template>
	<view class="teleplay-page">
		<image
			class="bg-linear-gradient"
			:src="OSSImgUrl(`bg-linear-gradient.png`)"
			mode="aspectFill"
		/>

		<view class="teleplay-title">
			<view class="title">
				<image
					style="height: 46rpx"
					mode="widthFix"
					:src="OSSImgUrl(`history-icon.png`)"
				/>
				<view class="txt">观看历史</view>
			</view>
			<view class="more"
				>全部
				<image
					class="icon"
					:src="OSSImgUrl(`icon_arrow.png`)"
					mode="widthFix"
				/>
			</view>
		</view>

		<view class="history-list-wrapper">
			<view class="no-data" v-if="historyList.length <= 0"> 暂无观看记录 </view>

			<scroll-view v-else class="history-list" scroll-x="true">
				<view
					class="history-item"
					v-for="(item, index) of historyList"
					:key="index"
					@click="() => gotoTeleplayDetail(item)"
				>
					<image class="cover" :src="item.cover_url" />
					<view class="title">{{ item.video_name }}</view>
					<view class="see-sort">观看至第{{ item.sort }}集</view>
				</view>
			</scroll-view>
		</view>

		<view class="teleplay-title">
			<view class="title">
				<image
					style="height: 54rpx"
					mode="widthFix"
					:src="OSSImgUrl(`collect-icon.png`)"
				/>
				<view class="txt">我的追剧</view>
			</view>
			<view class="more"
				>全部
				<image
					class="icon"
					:src="OSSImgUrl(`icon_arrow.png`)"
					mode="widthFix"
				/>
			</view>
		</view>

		<view class="collect-list-wrapper">
			<view class="no-data" v-if="collectList.length <= 0">
				<image
					mode="widthFix"
					class="video"
					:src="OSSImgUrl(`video-icon.png`)"
				/>
				<view class="txt">暂无观看记录</view>
				<image
					mode="widthFix"
					class="btn"
					:src="OSSImgUrl(`goto-see-video.png`)"
					@click="handleGotoSeeVideo"
				/>
			</view>

			<scroll-view v-else class="collect-list" scroll-y="true">
				<view
					class="collect-item"
					v-for="(item, index) of collectList"
					:key="index"
				>
					<image
						class="cover"
						:src="item.cover_url"
						@click="() => gotoTeleplayDetail(item)"
					/>
					<view class="collect-content">
						<view class="title">{{ item.video_name }}</view>
						<view class="count">更新至{{ item.count }}集</view>
						<view class="continue-see" @click="() => gotoTeleplayDetail(item)">
							<image mode="widthFix" :src="OSSImgUrl('jx-see-bg.png')" />
							<view class="txt">继续观看</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
import { getApiHistoryList, getApiUserCollectList } from '@/api/teleplay'
import { OSSImgUrl } from '@/utils'

export default {
	data() {
		return {
			guessList: [],
			historyList: [],
			eid: '',
			collectList: [],
		}
	},
	async onLoad() {
		this.eid = uni.getStorageSync('userInfo').eid
	},
	async onShow() {
		await this.getHistoryList()
		await this.getUserCollectList()
	},
	onHide() {},
	methods: {
		OSSImgUrl: OSSImgUrl,
		// 历史记录
		async getHistoryList() {
			// 获取eid传递参数
			const {
				data: { list },
			} = await getApiHistoryList(this.eid)
			this.historyList = list ? list : []
		},
		// 追剧列表
		async getUserCollectList() {
			const {
				data: { list },
			} = await getApiUserCollectList(this.eid, 1, 1000)
			this.collectList = list ? list : []
		},
		// 跳转播放页
		gotoTeleplayDetail(item) {
			console.log('🚀 ~ gotoTeleplayDetail ~ item:', item)
			// 储存剧集信息
			this.$store.commit('teleplay/setEpisodeInfo', item)
			if (tt.canIUse('PlayletExtension')) {
				tt.navigateTo({
					url: `/pages/playlet/index?seq=${item.sort}&tt_album_id=${item.album_id}&tt_episode_id=${item.episode_id}&is_continue=1`,
				})
			} else {
				uni.navigateTo({
					url: `/pages_sub1/media-player/index`,
				})
			}

			// uni.navigateTo({
			// 	url: `/pages_sub1/media-player/index`,
			// })
		},
		handleGotoSeeVideo() {
			uni.switchTab({
				url: `/pages/home/home`,
			})
		},
	},
}
</script>

<style lang="scss" scoped>
.teleplay-page {
	box-sizing: border-box;
	padding: 0rpx 40rpx;
	height: 100vh;

	.bg-linear-gradient {
		position: fixed;
		left: 0;
		top: 0;
		width: 100%;
		height: 400rpx;
		z-index: -1;
	}

	.teleplay-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		padding: 20rpx 0px;

		.title {
			display: flex;
			align-items: center;

			image {
				width: 46rpx;
				vertical-align: middle;
			}

			.txt {
				margin-left: 10rpx;
				font-size: 36rpx;
				font-weight: bold;
			}
		}

		.more {
			display: flex;
			align-items: center;
			font-size: 28rpx;
			color: #9da2a2;

			.icon {
				width: 32rpx;
				height: 32rpx;
			}
		}
	}

	.history-list-wrapper {
		.history-list {
			width: 100%;
			height: 350rpx;
			white-space: nowrap;

			.history-item {
				width: 180rpx;
				display: inline-flex;
				flex-direction: column;

				&:not(:last-child) {
					margin-right: 30rpx;
				}

				.cover {
					width: 100%;
					height: 240rpx;
					border-radius: 8px;
				}

				.title {
					font-size: 28rpx;
					font-weight: 600;
					white-space: nowrap; /* 禁止换行 */
					overflow: hidden; /* 超出部分隐藏 */
					text-overflow: ellipsis; /* 超出部分显示省略号 */
				}

				.see-sort {
					margin-top: 10rpx;
					font-size: 24rpx;
					color: #9da2a2;
					white-space: nowrap; /* 禁止换行 */
					overflow: hidden; /* 超出部分隐藏 */
					text-overflow: ellipsis; /* 超出部分显示省略号 */
				}
			}
		}

		.no-data {
			width: 100%;
			height: 340rpx;
			display: flex;
			justify-content: center;
			align-items: center;

			font-size: 34rpx;
			color: #a7a1a1;
		}
	}

	.collect-list-wrapper {
		height: calc(100% - 90rpx - 350rpx - 100rpx);

		.collect-list {
			width: 100%;
			height: 100%;
			padding-bottom: 20rpx;
			box-sizing: border-box;

			.collect-item {
				display: flex;
				&:not(:last-child) {
					margin-bottom: 30rpx;
				}

				.cover {
					width: 160rpx;
					height: 220rpx;
					border-radius: 8px;
				}

				.collect-content {
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					margin-left: 20rpx;

					.title {
						margin-top: 20rpx;
						font-size: 30rpx;
						white-space: nowrap; /* 禁止换行 */
						overflow: hidden; /* 超出部分隐藏 */
						text-overflow: ellipsis; /* 超出部分显示省略号 */
					}

					.count {
						font-size: 24rpx;
						color: #9da2a2;
						white-space: nowrap; /* 禁止换行 */
						overflow: hidden; /* 超出部分隐藏 */
						text-overflow: ellipsis; /* 超出部分显示省略号 */
					}

					.continue-see {
						width: 140rpx;
						height: 50rpx;
						margin-bottom: 30rpx;
						position: relative;

						.txt {
							position: absolute;
							top: 43%;
							left: 20%;
							transform: translateY(-50%);
							font-size: 22rpx;
							color: #fff;
						}

						image {
							width: 140rpx;
							height: 50rpx;
						}
					}
				}
			}
		}

		.no-data {
			margin-top: 50rpx;
			width: 100%;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			.video {
				width: 150rpx;
				height: 150rpx;
			}

			.txt {
				font-size: 34rpx;
				color: #a7a1a1;
				margin: 30rpx 0;
			}

			.btn {
				width: 210rpx;
				height: 72rpx;
			}
		}
	}
}

::v-deep {
	.u-line {
		border-color: #fff !important;
	}
}
</style>
